/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pingouin;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Hippolyte
 */
public class PanneauAccueil extends JPanel {
//    public static main m;

    public Graphics ag;
    private main m;
    private JComboBox nbJList;
    private JComboBox nbOList;
    private JComboBox nivList;
    private JCheckBox reseau;
    private JCheckBox rejoindreReseau;
    private JLabel addressLabel;
    private JTextField address;

    public PanneauAccueil(main main) {
//        super();
        
        System.out.println("dessine panneau accueil");
        
        m = main;
        
        setLayout(new GridLayout(19, 2));


        String[] nbJ = {"0", "1", "2", "3", "4"};
        nbJList = new JComboBox(nbJ);
        JLabel nbJLabel = new JLabel("nb joueurs reels");

        String[] nbO = {"0", "1", "2", "3", "4"};
        nbOList = new JComboBox(nbO);
        JLabel nbOLabel = new JLabel("nb ordi");

        String[] niv = {"1", "2", "3", "4", "5", "6"};
        nivList = new JComboBox(niv);
        JLabel nivLabel = new JLabel("niveau ordi");


        add(nbJLabel);
        add(nbJList);

        add(nbOLabel);
        add(nbOList);

        add(nivLabel);
        add(nivList);

        ActionListener alReseau = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                address.setVisible(rejoindreReseau.isSelected());
                addressLabel.setVisible(rejoindreReseau.isSelected());
            }
        };

        JLabel reseauLabel = new JLabel("creer partie reseau");
        reseau = new JCheckBox();

        add(reseauLabel);
        add(reseau);


        JLabel rejoindreReseauLabel = new JLabel("rejoindre partie");
        rejoindreReseau = new JCheckBox();

        add(rejoindreReseauLabel);
        add(rejoindreReseau);

        rejoindreReseau.addActionListener(alReseau);

        addressLabel = new JLabel("adresse");
        address = new JTextField();

        addressLabel.setVisible(false);
        address.setVisible(false);

        add(addressLabel);
        add(address);



        JPanel buttonPanel = new JPanel();

        JButton btnOk = new JButton("Commencer");

        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String addressIp = address.getText();

                if (addressIp != null && !addressIp.isEmpty()) {

                    System.out.println(addressIp);

                    m.rejoindrePartie(addressIp);

                } else {

                    boolean res = reseau.isSelected();

                    int nbJoueurs = Integer.parseInt((String) nbJList.getSelectedItem());
                    int nbOrdis = Integer.parseInt((String) nbOList.getSelectedItem());
                    int niveau = Integer.parseInt((String) nivList.getSelectedItem());

                    int nbTotal = nbJoueurs + nbOrdis;



                    if (nbTotal > 0 && nbTotal < 5) {
                        m.commencerPartie(nbJoueurs, nbOrdis, niveau, res);
                        System.out.println("lance partie");
                    }

                }
            }
        };

        btnOk.addActionListener(al);

        buttonPanel.add(btnOk);

        add(buttonPanel);

        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());
        add(new JLabel());

    }

    @Override
    public void paintComponent(Graphics g) {
        
        System.out.println("paintComponent");
        
        ag = g;

        
    }

    public void ecrireMessage(String s, int xString, int yString) {
        ag.drawString(s, xString, yString);
    }
}
